<?php

class CodeSpace_Language_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract{
    
    private $_options = null;
    
    public function __construct($options)
    {
        $this->_options = $options;
    }
    
    public function routeStartup (Zend_Controller_Request_Abstract $request)
    {
        if (substr($request->getRequestUri(), 0, -1) == $request->getBaseUrl()){
            $request->setRequestUri($request->getRequestUri()."tr_TR"."/");
            $request->setParam("language", "tr_TR");
        }
    }
 
    public function routeShutdown (Zend_Controller_Request_Abstract $request)
    {
        //var_dump($request->getParams());
        //exit;
        $language =  $request->getParam("language");
        if($language==''){
            $language = 'tr_TR';
        }
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $view = $viewRenderer->view;
        
        if(!$view->language()->setLanguage($language))
        {
            throw new Exception('Geçersiz dil');
        }
        
        $locale = new Zend_Locale($language);
        $locale->setLocale($locale);
        Zend_Registry::set('Zend_Locale', $locale);

        $locale = Zend_Registry::get('Zend_Locale');    
        
        $translateResource = new Zend_Config_Ini(APPLICATION_PATH . '/configs/language.ini', 'translate');
        $translateResource = new Zend_Translate($translateResource);
        
        foreach($view->language()->getLanguages() as $langs)
        {
            $lang = array(
                'content' => APPLICATION_PATH . '/../data/locales/',
               	'scan' => 'directory',
                'locale' => $langs
            );
            $translateResource->addTranslation($lang);
        }
        
        $translateResource->setLocale($locale);

        Zend_Registry::set('Zend_Translate', $translateResource);
        
        Zend_Controller_Router_Route::setDefaultTranslator($translateResource);

        $front = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();
        $router->setGlobalParam('language', $language);

        Zend_Controller_Action_HelperBroker::addHelper(new CodeSpace_Language_Controller_Helper_Language()
);
    }
    
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $view = $viewRenderer->view;
        
        $view->placeholder("languageMenu")->captureStart();
            include $this->_options['viewpath'] . "/language/languageMenu.phtml";
        $view->placeholder("languageMenu")->captureEnd();
        
        if(APPLICATION_ENV === 'development' || APPLICATION_ENV === 'testing')
        {
            $view->placeholder("languageProfiler")->captureStart();
                include $this->_options['viewpath'] . "/language/languageProfiler.phtml";
            $view->placeholder("languageProfiler")->captureEnd();
        }
    }
}

?>